<template>
	<view>
		<view class="CommissionRank">
			<view class="header">
				<view class="rank" v-if="position">您目前的排名<text class="num">{{position}}</text>名</view>
				<view class="rank" v-else>您目前暂无排名</view>
			</view>
			<view class="wrapper">
				<view class="nav acea-row row-around">
					<view class="item" :class="active === index ? 'font-color' : ''" v-for="(item,index) in navList"
                :key="index" @click="switchTap(index)">
						{{ item }}
					</view>
				</view>
				<view class="list">
					<view class="item acea-row row-between-wrapper" v-for="(item,index) in rankList" :key="index">
						<view class="num" v-if="index <= 2">
							<image :src="'/static/images/medal0' + (index+1) + '.png'" />
						</view>
						<view class="num" v-else>
							{{index + 1}}
						</view>
						<view class="picTxt acea-row row-between-wrapper">
							<view class="pictrue">
								<image :src="item.avatar"></image>
							</view>
							<view class="text line1">{{ item.nickname }}</view>
						</view>
						<view class="people font-color">￥{{ fen2yuan(item.brokeragePrice) }}</view>
					</view>
				</view>
				<view class='noCommodity' v-if="rankList.length === 0 && (page !== 1 || active === 0)">
					<emptyPage title="暂无排行～"></emptyPage>
				</view>
			</view>
		</view>
		<home></home>
	</view>
</template>
<script>
	import { toLogin } from '@/libs/login.js';
	import home from '@/components/home';
	import emptyPage from '@/components/emptyPage.vue'
	import { mapGetters } from "vuex";
  import * as BrokerageAPI from '@/api/trade/brokerage.js'
  import dayjs from "@/plugin/dayjs/dayjs.min.js";
  import * as DateUtil from '@/utils/date.js';
  import * as Util from '@/utils/util.js';
  export default {
		components: {
			emptyPage,
			home,
		},
		data() {
			return {
				navList: ["周排行", "月排行"],
				active: 0,
				rankList: [],
				page: 1,
				limit: 20,
				loadend: false,
				loading: false,
				loadTitle: '加载更多',
				type: 'week',
				position: 0,
			};
		},
		computed: mapGetters(['isLogin']),
		watch:{
			isLogin:{
				handler:function(newV, oldV) {
					if (newV) {
						this.getBrokerageRankList();
						this.getBrokerageRankNumber(this.type);
					}
				},
				deep:true
			}
		},
		onLoad() {
			if (!this.isLogin) {
        toLogin();
        return;
			}
      this.calculateTimes();
      this.getBrokerageRankList();
      this.getBrokerageRankNumber(this.type);
		},
		methods: {
			switchTap: function(index) {
				this.active = index;
				this.type = index ? 'month' : 'week';
				this.page = 1;
				this.loadend = false;
				this.$set(this, 'rankList', []);
        this.calculateTimes();
				this.getBrokerageRankList();
				this.getBrokerageRankNumber(this.type);
			},
			getBrokerageRankNumber() {
        BrokerageAPI.getBrokerageUserRankByPrice({
          // 'startTime': this.times[0],
          // 'endTime': this.times[1],
          'times': this.times
				}).then(res => {
					this.position = res.data;
				})
			},
			getBrokerageRankList: function() {
				if (this.loadend || this.loading) {
          return;
        }
				this.loading = true;
				this.loadTitle = '';
        BrokerageAPI.getBrokerageUserRankPageByPrice({
					pageNo: this.page,
					pageSize: this.limit,
          'times[0]': this.times[0],
          'times[1]': this.times[1],
				}).then(res => {
					let list = res.data.list;
					let loadend = list.length <= this.limit;
					this.rankList.push.apply(this.rankList, list);
					this.loading = false;
					this.loadend = loadend;
					this.loadTitle = loadend ? '😕我也是有底线的' : '加载更多';
					this.$set(this, 'rankList', this.rankList);
				}).catch(err => {
					this.loading = false;
					this.loadTitle = '加载更多';
				})
			},
      calculateTimes: function() {
        let times;
        if (this.type === 'week') {
          times = DateUtil.getWeekTimes();
        } else {
          times = DateUtil.getMonthTimes();
        }
        this.times = [ this.formatDate(times[0]), this.formatDate(times[1]) ]
      },
      formatDate: function(date) {
        return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
      },
      fen2yuan(price) {
        return Util.fen2yuan(price)
      },
		},
		onReachBottom: function() {
			this.getBrokerageRankList();
		}
	}
</script>

<style scoped lang="scss">
	.CommissionRank .header {
		background: url("") no-repeat;
		width: 100%;
		height: 344rpx;
		background-size: 100% 100%;
	}

	.CommissionRank .header .rank {
		font-size: 33rpx;
		color: #fff;
		position: absolute;
		top: 160rpx;
		left: 48rpx;
	}

	.CommissionRank .header .rank .num {
		font-size: 51rpx;
		font-weight: bold;
		margin: 0 10rpx;
	}

	.CommissionRank .wrapper {
		width: 710rpx;
		background-color: #fff;
		border-radius: 14rpx;
		margin: -76rpx auto 0 auto;
	}

	.CommissionRank .wrapper .nav {
		height: 99rpx;
		border-bottom: 2.5rpx solid #f3f3f3;
		font-size: 30rpx;
		font-weight: bold;
		color: #999;
		line-height: 99rpx;
	}

	.CommissionRank .wrapper .nav .item.font-color {
		border-bottom: 4rpx solid $theme-color;
		margin-top: -2rpx;
	}

	.CommissionRank .wrapper .list {
		padding: 0 30rpx;
	}

	.CommissionRank .wrapper .list .item {
		border-bottom: 1px solid #f3f3f3;
		height: 101rpx;
		font-size: 28rpx;
	}

	.CommissionRank .wrapper .list .item .num {
		color: #666;
		width: 70rpx;
	}

	.CommissionRank .wrapper .list .item .num image {
		width: 34rpx;
		height: 40rpx;
		display: block;
	}

	.CommissionRank .wrapper .list .item .picTxt {
		width: 350rpx;
	}

	.CommissionRank .wrapper .list .item .picTxt .pictrue {
		width: 68rpx;
		height: 68rpx;
	}

	.CommissionRank .wrapper .list .item .picTxt .pictrue image {
		width: 100%;
		height: 100%;
		display: block;
		border-radius: 50%;
	}

	.CommissionRank .wrapper .list .item .picTxt .text {
		width: 262rpx;
		color: #333;
	}

	.CommissionRank .wrapper .list .item .people {
		width: 175rpx;
		text-align: right;
	}
</style>
